package org.xyl;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.xyl.dto.RAGRequest;
import org.xyl.dto.RAGResponse;
import org.xyl.engine.EnterpriseRAGService;
import org.xyl.engine.EnterpriseRAGServiceImpl;
import org.xyl.service.VectorLibraryService;
import org.xyl.service.VectorLibraryServiceInterface;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * RAG应用主类
 * 企业级RAG系统的入口点
 */
@SpringBootApplication(exclude = {
        org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class
})
@RestController
@EnableAsync  // 启用异步处理支持
public class RAGApplication {

    @Autowired
    private EnterpriseRAGService ragService;
    
    @Autowired
    private VectorLibraryServiceInterface vectorLibraryService;
    
    /**
     * 应用程序入口
     */
    public static void main(String[] args) {
        SpringApplication.run(RAGApplication.class, args);
    }
    
    /**
     * 配置异步任务执行器
     */
    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        // 创建线程池，用于处理异步任务
        return Executors.newFixedThreadPool(10);
    }
    
    public static class DocumentRequest {
        private String documentPath;
        
        public String getDocumentPath() {
            return documentPath;
        }
        
        public void setDocumentPath(String documentPath) {
            this.documentPath = documentPath;
        }
    }
    
    /**
     * RAG查询接口 - 保持向后兼容
     */
    @PostMapping("/api/rag/query")
    public ResponseEntity<RAGResponse> ragQuery(@RequestBody RAGRequest request) {
        RAGResponse response = ragService.ragQuery(request.getQuestion(), request.getVectorLibraryName());
        return ResponseEntity.ok(response);
    }

    /**
     * 文档处理接口 - 保持向后兼容
     */
    @PostMapping("/api/rag/ingest")
    public ResponseEntity<String> ingestDocument(@RequestBody DocumentRequest request) {
        // 文档入库接口
        ragService.processDocument(request.getDocumentPath());
        return ResponseEntity.ok("文档处理完成");
    }

    // 异步处理提升性能
    @Async
    public CompletableFuture<String> asyncRagQuery(String question) {
        return CompletableFuture.supplyAsync(() -> ragQuery(new RAGRequest(question)).getBody().getAnswer());
    }

    // 缓存机制
    @Cacheable(value = "ragCache", key = "#question")
    public String cachedRagQuery(String question) {
        RAGRequest request = new RAGRequest();
        request.setQuestion(question);
        return ragQuery(request).getBody().getAnswer();
    }

    // 批量文档处理
    public void batchProcessDocuments(List<String> documentPaths) {
        documentPaths.parallelStream()
                .forEach(path -> ragService.processDocument(path));
    }
}

